package com.example.a24porint;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class zhucewindoow extends AppCompatActivity {
    public Socket socket=null;
    private OutputStream outputStream;
    private InputStream inputStream;
    private EditText accountNumber;
    private EditText accountPassword;
    private Button querenzhuce;
    private BufferedReader in;
    private boolean isregyes(String s)
    {
        String str="regyes";
        if(s.length()<str.length())return false;
        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)!=s.charAt(i))
            {
                return false;
            }
        }
        return true;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhucewindoow);
        accountNumber=findViewById(R.id.editTextTextPersonName);
        accountPassword=findViewById(R.id.editTextTextPersonName2);
        querenzhuce=findViewById(R.id.button16);
        ApplicationUtil appUtil =  (ApplicationUtil) zhucewindoow.this.getApplication();
        try {
            appUtil.init();
            socket = appUtil.getSocket();
            outputStream=appUtil.getOutputStream();
            inputStream=appUtil.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("initerrortag","wjoafjaoisdjfoiasdjf--------------------");
        }
        querenzhuce.setOnClickListener(new View.OnClickListener() {
            final String[] receiveMsg = new String[1];
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String str="register";
                            str+=accountNumber.getText().toString();
                            str+=';';
                            str+=accountPassword.getText().toString();
                            byte[] str_head=str.getBytes();
                            outputStream.write(str_head);
                            in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                            while (true) {
                                receiveMsg[0] = in.readLine();
                                if (receiveMsg[0] != null && isregyes(receiveMsg[0])) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(zhucewindoow.this, "注册成功！！请点击返回键返回", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                    break;
                                }
                            }
                            Log.i("wintag","winisok");
                    /*in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
                    receiveLoginMsg();
                    initgame();*/
                            //initLoginThread();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

    }
}